Content:
Import Libraries
In [1]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
Import data
In [2]:
tips = sns.load_dataset('tips')
tips.head()
Out[2]:
In [3]:
# Iris data in wide-form
iris = sns.load_dataset('iris')
iris.head()
Out[3]:
In [4]:
# Melt iris data to long-form
irislong = pd.melt(iris,id_vars='species', var_name='measurement')
irislong.head()
Out[4]:
In [5]:
# Plotting using wide-form iris data, each numeric column will be plotted
sns.stripplot(data=iris);
In [6]:
# Plottin using long-form iris data, indicate x, y values
sns.stripplot(x='measurement',y='value',data=irislong);
In [7]:
# Long-form of data is preferred to take advantage of hue parameter, which allows visualisation of a second categorical variable
sns.stripplot(x='measurement',y='value',data=irislong,hue='species',jitter=0.05);
In [8]:
sns.swarmplot(x='measurement',y='value',hue='species',data=irislong);
In [9]:
# Another example using Tips data set
sns.swarmplot(x="day", y="total_bill", data=tips);
In [10]:
sns.swarmplot(x="day", y="total_bill", hue='smoker', data=tips);
In [11]:
sns.boxplot(x='measurement',y='value',data=irislong);
In [12]:
sns.boxplot(x='measurement',y='value',hue='species',data=irislong);
Combining boxplot with swarmplot
In [13]:
sns.set_style("whitegrid")
sns.boxplot(x='measurement',y='value',data=irislong);
sns.swarmplot(x='measurement',y='value',data=irislong,color='0.4',alpha=0.75);
In [14]:
sns.violinplot(x='measurement',y='value',data=irislong);
In [15]:
sns.violinplot(x='measurement',y='value',hue='species',data=irislong);
In [16]:
sns.violinplot(x='day',y='tip',data=tips);
In [17]:
# Split the violin into half for a second categorical (binary) variable.
ax = sns.violinplot(x='day',y='tip',data=tips,hue='sex',split=True) # Use dodge instead of split for new version 8.0
ax.legend(bbox_to_anchor=(1,0.5));
In [18]:
# Customising the inner of violinplot
# Default: Box
# Other choices: point, quartile, stick
sns.violinplot(x='day',y='tip',data=tips,inner='point');
Combining violinplot with swarmplot.
In [53]:
sns.violinplot(x='day',y='tip',data=tips,inner=None)
sns.swarmplot(x='day',y='tip',data=tips,color='k',alpha=0.5);
In [19]:
# Count the frequency of each categorical variable
sns.countplot(x='day',data=tips);
In [12]:
# Display the count for each bar
ax = sns.countplot(x='day',data=tips);
for p in ax.patches:
h = p.get_height()
ax.text(p.get_x()+p.get_width()/2, h+2, '{:1.0f}'.format(h), ha="center")
ax.set_title('Number of customers per day');
In [15]:
# Average of a numerical variable, with CI (default 95%)
ax = sns.barplot(x='day',y='tip',data=tips)
ax.set_title('Average tip per day');
In [35]:
ax = sns.barplot(x='day',y='tip',data=tips,hue='time',capsize=0.1,ci=90)
ax.set_title('Average tip per day')
# Display legend outside the axes
ax.legend(bbox_to_anchor=(1,0.5),title='Time');
In [8]:
sns.pointplot(x='day',y='tip',data=tips);
In [10]:
sns.pointplot(x='day',y='tip',hue='time',data=tips,dodge=True);
In [ ]: